package com.edu.service.impl;

import com.edu.bean.User;
import com.edu.mapper.UserMapper;
import com.edu.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public User selectUserByName(String userName) {
        return userMapper.selectUserByName(userName);
    }

    @Override
    public String register(User user) {
        if (userMapper.selectUserByName(user.getUserName()) == null) {
            if (userMapper.insert(user) > 0) {
                return "注册成功";
            }
            return "注册失败";
        }
        return "账号存在";
    }

    @Override
    public Map<String, Object> login(User user) {
        Map<String,Object> userMap = new HashMap<>();
        System.out.println(userMapper.selectUserByName(user.getUserName()));
        if (userMapper.selectUserByName(user.getUserName()) != null) {
            User user1 = userMapper.selectUserByName(user.getUserName());
            if (user1.getUserPass().equals(user.getUserPass())) {
                userMap.put("msg",user1);
            } else {
                userMap.put("msg", "密码错误");
            }
        } else {
            userMap.put("msg", "账户不存在");
        }
        return userMap;
    }

    @Override
    public String changePass(User user, String newPass) {
        if (userMapper.selectUserByName(user.getUserName()) != null) {
            User user1 = userMapper.selectUserByName(user.getUserName());
            if (user1.getUserPass().equals(user.getUserPass())) {
                user.setUserPass(newPass);
                int row = userMapper.updateByUserName(user);
                if (row > 0) {
                    return "修改成功";
                } else {
                    return "修改失败";
                }
            } else {
                return "老密码错误";
            }
        } else {
            return "用户不存在";
        }
    }
}
